﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AngleExpress.viewmodel
{
    class ViewCommand : ICommand
    {
        readonly Func<bool> _canExecute;
        readonly Action<string> _execute;


        public ViewCommand(Action<string> action):this(action,null)
        {
        }

        public ViewCommand(Action<string> action, Func<bool> func)
        {
            this._canExecute = func;
            this._execute = action;
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public void Execute(object parameter)
        {
            _execute(parameter.ToString());
        }
    }
}
